﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BGSpaceShooter
{
    public abstract class GameObject : IDrawable
    {
        public GameObject(Model model)
        {
            Model = model;

            Textures = new List<Texture2D>();

            Ambient = new List<float>();
            Opacity = new List<float>();
            Transparency = new List<float>();
            DiffuseColor = new List<Vector3>();
            DiffuseFactor = new List<float>();
            Specular = new List<Vector3>();
            SpecularFactor = new List<float>();
            Shininess = new List<Vector3>();
            Opacity = new List<float>();

            Scale = 1;

            BoundingSphere = CalculateBoundingSphere();

            LastHPUpdate = DateTime.Now;
        }
        #region IDrawable Members

        public virtual IDrawer GetDrawer()
        {
            throw new NotImplementedException();
        }

        #endregion
        public Vector3 Velocity { get; set; }
        public Vector3 Position { get; set; }
        public float Scale { get; set; }
        public Matrix RotationMatrix { get; set; }
        public DateTime LastUpdate { get; set; }
        public List<Texture2D> Textures { get; set; }
        public List<float> Opacity { get; set; }
        public List<float> Transparency { get; set; }
        public List<Vector3> DiffuseColor { get; set; }
        public List<float> DiffuseFactor { get; set; }
        public List<float> Ambient { get; set; }
        public List<Vector3> Specular { get; set; }
        public List<Vector3> Shininess { get; set; }
        public List<float> SpecularFactor { get; set; }
        public BoundingSphere BoundingSphere { get; set; }
        public DateTime LastHPUpdate { get; set; }
        public int HP { get; set; }
        public int MaxHP { get; set; }
        public Model Model { get; set; }

        public Matrix AdditionalTransformations
        {
            get
            {
                return Matrix.CreateRotationX(MathHelper.PiOver2) * Matrix.CreateRotationZ(MathHelper.Pi);
            }
        }



        public Matrix ModelMatrix
        {
            get
            {
                return Matrix.Identity * AdditionalTransformations *
                       RotationMatrix * Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position);
            }
        }


        private BoundingSphere CalculateBoundingSphere()
        {
            Vector3 center = Vector3.Zero;
            float radius = 0;
            foreach (ModelMesh modelMesh in Model.Meshes)
            {
                float dist = modelMesh.BoundingSphere.Center.Length() + modelMesh.BoundingSphere.Radius;

                if (dist > radius)
                {
                    radius = dist;
                }
            }
            return new BoundingSphere(center, radius * Scale);
        }

        public virtual IEnumerable<ParticleSystem.IObjectEffect> GetEffectsToDraw()
        {
            return new List<ParticleSystem.IObjectEffect>();
        }

    }
}
